# -*- coding:UTF-8 -*-
from selenium import webdriver

allData = []
# webdriver 驱动路径
path = 'E:/desktop/chromedriver_win32/chromedriver.exe'
driver = webdriver.Chrome(executable_path=path)
url = 'https://www.6baike.com/wiki/HuNanLiShi/index.html'
driver.get(url)


def get_event():
    history_list = driver.find_elements_by_css_selector('#content > div.cwz.list li a')
    length = history_list.__len__()
    for i in range(length):
        history_list = driver.find_elements_by_css_selector('#content > div.cwz.list li a')
        a = history_list[i]
        text = a.text
        if '历史大事' in text:
            print(text)
            try:
                a.click()
            except Exception as e:
                print(e)
                continue
            event_names = {}
            try:
                event_names = driver.find_elements_by_css_selector('#content > div.arc > li')
            except Exception as e:
                print(e)
                continue
            for item in event_names:
                allData.append({text: item.text})
            driver.back()


get_event()
two = driver.find_element_by_css_selector('#content > div.cwz.list > div.listfy > a:nth-child(3)')
two.click()
get_event()

print(allData)

writeData=open('event.txt','w')
writeData.writelines(str(allData))
writeData.close()